package xdu.lz.stage5_jucUtils.chapter5_lock.readwritelock;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created at 2019/10/19 0019 下午 9:18
 *
 * 读可以并行，写串行
 *
 * 这样实现不了
 */
public class ReadWriteLock1 {

    private final static ReentrantLock lock = new ReentrantLock(true);

    private final static List<Long> data = new ArrayList<>();

    public static void main(String[] args) {

    }

    public static void write(){
        try {
            lock.lock();
            Thread.sleep(1000);
            data.add(System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public static void read(){
        try {
            lock.lock();
            Thread.sleep(1000);
            data.forEach(System.out::println);
            System.out.println(Thread.currentThread().getName()+" read the data ==========");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}
